//
//  CRCommentCell.m
//  CRChatRoom
//
//  Created by lanou3g on 15/11/27.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//
#define lableFont [UIFont systemFontOfSize:14]
#import "CRSpaceCommentCell.h"
#import "UIImageView+WebCache.h"
#import "CRSpaceComment.h"
#define margin 5

@interface CRSpaceCommentCell ()

/**
 *  评论人的头像
 */
@property (nonatomic, strong) UIImageView *headerView;
/**
 *  评论人的username
 */
@property (nonatomic, strong) UILabel *nameLable;
/**
 *  评论的时间
 */
@property (nonatomic, strong) UILabel *timeLable;
/**
 *  评论的内容
 */
@property (nonatomic, strong) UILabel *contentLable;
/**
 *  cell的高度
 */
@property (nonatomic, assign) CGFloat cellHeight;

/**
 *  第几楼
 */
@property (nonatomic, strong) UILabel *positionLabel;

@end

@implementation CRSpaceCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setSubs];
}
    return self;
}
static CGFloat height = 0;



- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self setupSubView];

}
- (void)setSubs
{
    self.headerView = [[UIImageView alloc] init];
    self.headerView.layer.cornerRadius = 20;
    self.headerView.clipsToBounds = YES;
    self.nameLable = [[UILabel alloc] init];
    self.timeLable = [[UILabel alloc] init];
    self.contentLable = [[UILabel alloc] init];
    self.nameLable.font = lableFont;
    self.timeLable.font = [UIFont systemFontOfSize:12];
    self.contentLable.font = lableFont;
    self.contentLable.numberOfLines = 0;
    self.positionLabel = [[UILabel alloc] init];
    self.positionLabel.font = [UIFont systemFontOfSize:8];
    self.positionLabel.textColor = [UIColor colorWithRed:0.145 green:0.391 blue:0.078 alpha:1.000];

    
    [self.contentView addSubview:self.headerView];
    [self.contentView addSubview:self.nameLable];
    [self.contentView addSubview:self.timeLable];
    [self.contentView addSubview:self.contentLable];
    [self.contentView addSubview:self.positionLabel];
    

}
- (void)setupSubView
{
    self.headerView.frame = CGRectMake(5, 5, 40, 40);
    self.nameLable.frame = CGRectMake(50, 5, self.width - 120, 25);
    self.timeLable.frame = CGRectMake(self.width - 60, 5, 50, 25);
    self.positionLabel.frame = CGRectMake(self.width - 30, 5, 20, 10);
}


// 重写模型的set方法
- (void)setComment:(CRSpaceComment *)comment
{
    
    _comment = comment;
    [self.headerView sd_setImageWithURL:[NSURL URLWithString:comment.iconUrl]];
    self.nameLable.text = comment.username;
    self.timeLable.text = comment.time;
    self.contentLable.text = comment.content;
    self.positionLabel.text = [NSString stringWithFormat:@"%@楼", comment.position];

    CGSize contentSize = [CRSpaceCommentCell sizeWithText:comment.content andMaxSize:CGSizeMake(self.width - 60, MAXFLOAT) andFont:lableFont];
    self.contentLable.frame = CGRectMake(50, 35, self.width - 60, contentSize.height);

    self.cellHeight = contentSize.height + 40;
    height = self.cellHeight;

}

+ (CGSize)sizeWithText:(NSString *)text andMaxSize:(CGSize)maxSize andFont:(UIFont *)font
{
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
}

+ (CGFloat)hieghtForCell
{
    return height + margin;
}



// 重新设置cell的frame
- (void)setFrame:(CGRect)frame
{
    
    frame.origin.y += margin;
    frame.origin.x = margin;
    frame.size.width -= 2 * margin;
    frame.size.height -= margin;
    
    
    
    [super setFrame:frame];
}


@end
